package com.liu.QQ.client.view;

import com.liu.QQ.client.service.MessageClientService;
import com.liu.QQ.client.service.UserClientService;
import com.liu.QQ.utils.Utility;

import java.io.IOException;

/**
 * 登录界面
 * @author liuchengjie
 */
@SuppressWarnings({"all"})
public class QQview {


    private boolean loop = true;
    private UserClientService userClientService = new UserClientService();
    private MessageClientService messageClientService = new MessageClientService();
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        new QQview().mainMenu();
    }
    /**
     * 显示主菜单
     */
    private void mainMenu() throws IOException, ClassNotFoundException {
        while (loop) {
            System.out.println("==========欢迎登录网络通讯系统");
            System.out.println("1 表示登录系统");
            System.out.println("9 表示退出系统");
            System.out.print("请输入您的选择：");
            String key = Utility.readString(1);

            switch (key) {
                case "1":
                    System.out.print("请输入用户id:");
                    String userId = Utility.readString(50);
                    System.out.print("请输入密码:");
                    String password = Utility.readString(50);
                    //调用验证服务
                    boolean b = userClientService.checkUser(userId, password);
                    if (b) {
                        //登录成功
                        System.out.println("=======欢迎(" + userId + ")登录成功============");
                        System.out.println("网络通讯系统二级菜单");
                        while (loop) {
                            System.out.println("\n=========网络通信系统二级菜单：用户(" + userId + ")");
                            System.out.println("\t\t 1 显示在线用户列表");
                            System.out.println("\t\t 2 群发消息");
                            System.out.println("\t\t 3 私聊消息");
                            System.out.println("\t\t 4 发送文件");
                            System.out.println("\t\t 9 退出系统");
                            System.out.println("请输入您的选择：");
                            String sign = Utility.readString(1);
                            switch (sign) {
                                case "1":
                                    System.out.println("显示用户列表");
                                    //方法获取在线用户列表
                                    userClientService.onlineFriendList();
                                    break;
                                case "2":
                                    System.out.println("请输入群发消息的内容(在100个字符以内)：");
                                    String distribution = Utility.readString(100);
                                    messageClientService.distributionMessage(userId,distribution);
                                    //编写方法
                                    break;
                                case "3":
                                    System.out.println("请输入私聊的用户号（对方必须在线）:");
                                    String getterId = Utility.readString(50);
                                    System.out.println("请输入聊天内容（100字符以内）:");
                                    String content = Utility.readString(100);
                                    //调用私聊方法
                                    messageClientService.sendMessageToOne(userId,getterId,content);
                                    break;
                                case "4":
                                    System.out.println("发送文件");
                                    break;
                                case "9":
                                    System.out.println("退出系统");
                                    //调用方法给服务端关闭当前socket的消息
                                    userClientService.ExitClientSystem();
                                    loop = false;
                                    break;
                                default:
                                    System.out.println("输入错误，请重新输入");
                                    break;
                            }
                        }
                    } else {
                        System.out.println("登录失败");
                    }
                    break;
                case "9":
                    System.out.println("退出系统");
                    loop = false;
                    break;
                default:
                    System.out.println("输入错误，请重新输入");
                    break;
            }

        }
    }
}
